<template>
	<div>
		<compWrapper title="基本信息" theme="primary">
			<compFormQuery v-if="type==='query'" class="margin-top-15" :formdata="getBase" :value="formData"></compFormQuery>
            <compFormEdit v-else ref="formbase" class="margin-top-15" :formdata="getBase" v-model="formData"></compFormEdit>
		</compWrapper>
		<div v-if="type!='query'" class="text-right margin-top-10">
			<Button type="primary" @click="submit">保存</Button>
			<Button class="margin-left-10" @click="onCancel">返回</Button>
		</div>
		<Spin v-if="loading" fix></Spin>
	</div>
</template>

<script>
import { HttpBaseSettingOne, HttpSaveBaseSetting } from '@/service/api/views';
import { mapGetters } from "vuex";
export default {
	props:['querytype','activeid'],
	data() {
		return {
            type: this.querytype || this.$route.params.type,
            id: this.activeid || this.$route.query.id,
            loading: false,
            formData: {
                orgId:'',
                consumeType:'',
                billType:''
            }
		};
	},
	created() {
		console.log(this.type,'typetype')
        this.getDetail();

	},
	computed: {
         ...mapGetters('dictionary',['getSysAllList']),
		getBase() {
			return [
                {
                    label: '客户组织',
                    tag: 'Select',
                    key: 'orgId',
                    options:this.getSysAllList,
					props:{
                        filterable:true,
                    },
                },
                {
                    label: '消费模式',
                    tag: 'Select',
                    key: 'consumeType',
                    options:[
                        {value:1,label:'后付费'},
                        {value:2,label:'预充值'},
                    ],
                },
                {
                    label: '出账类型',
                    tag: 'Select',
                    key: 'billType',
                    options:[
                        {value:1,label:'实际账单'},
                        {value:2,label:'系统报价'},
                    ],
                },
			];
		},
	},
	methods: {
		onCancel() {
			this.$router.push({
				name: "settingOrgset"
			});
		},
		getDetail() {
			if (this.type === "add") return;
			this.loading = true;
			HttpBaseSettingOne({
				orgId: this.id,
			}).then(res => {
                this.formData = res.data;
                this.loading = false;
			}, err => {
				this.loading = false;
				this.$Message.error(err.message);
			});
		},
		async submit() {
			const valid = await this.$refs.formbase.validate();
			if (!valid) return;
			this.loading = true;
			HttpSaveBaseSetting({
                ...this.formData,
			}).then(res => {
				this.loading = false;
                this.$Message.success(res.message)
				this.onCancel();
			}, err => {
				this.loading = false;
				this.$Message.error(err.message);
			})
		},
	}
};
</script>
